'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('purchase_orders', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      order_number: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '采购订单号'
      },
      supplier_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '供应商ID'
      },
      purchaser: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '采购员'
      },
      order_date: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '订单日期'
      },
      expected_delivery_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '预计交货日期'
      },
      total_amount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
        comment: '总金额'
      },
      status: {
        type: Sequelize.STRING(20),
        defaultValue: 'pending',
        allowNull: false,
        comment: '状态: pending待处理, confirmed已确认, completed已完成, cancelled已取消'
      },
      remark: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注'
      },
      organization_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '组织ID'
      },
      created_at: {
        type: Sequelize.DATE
      },
      updated_at: {
        type: Sequelize.DATE
      }
    });

    // 添加索引
    await queryInterface.addIndex('purchase_orders', ['order_number'], {
      name: 'idx_purchase_orders_order_number'
    });

    await queryInterface.addIndex('purchase_orders', ['supplier_id'], {
      name: 'idx_purchase_orders_supplier_id'
    });

    await queryInterface.addIndex('purchase_orders', ['organization_id'], {
      name: 'idx_purchase_orders_organization_id'
    });

    await queryInterface.addIndex('purchase_orders', ['status'], {
      name: 'idx_purchase_orders_status'
    });

    await queryInterface.addIndex('purchase_orders', ['order_date'], {
      name: 'idx_purchase_orders_order_date'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('purchase_orders');
  }
};'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('purchase_orders', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      order_number: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '采购订单号'
      },
      supplier_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '供应商ID'
      },
      purchaser: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '采购员'
      },
      order_date: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '订单日期'
      },
      expected_delivery_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '预计交货日期'
      },
      total_amount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
        comment: '总金额'
      },
      status: {
        type: Sequelize.STRING(20),
        defaultValue: 'pending',
        allowNull: false,
        comment: '状态: pending待处理, confirmed已确认, completed已完成, cancelled已取消'
      },
      remark: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注'
      },
      organization_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '组织ID'
      },
      created_at: {
        type: Sequelize.DATE
      },
      updated_at: {
        type: Sequelize.DATE
      }
    });

    // 添加索引
    await queryInterface.addIndex('purchase_orders', ['order_number'], {
      name: 'idx_purchase_orders_order_number'
    });

    await queryInterface.addIndex('purchase_orders', ['supplier_id'], {
      name: 'idx_purchase_orders_supplier_id'
    });

    await queryInterface.addIndex('purchase_orders', ['organization_id'], {
      name: 'idx_purchase_orders_organization_id'
    });

    await queryInterface.addIndex('purchase_orders', ['status'], {
      name: 'idx_purchase_orders_status'
    });

    await queryInterface.addIndex('purchase_orders', ['order_date'], {
      name: 'idx_purchase_orders_order_date'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('purchase_orders');
  }
};